package cst.a903dss.zyfypt_903dss.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import cst.a903dss.zyfypt_903dss.AmicoolListener;
import cst.a903dss.zyfypt_903dss.Bean.RegBean;
import cst.a903dss.zyfypt_903dss.Judgement;
import cst.a903dss.zyfypt_903dss.R;
import cst.a903dss.zyfypt_903dss.ResModel;

public class RegActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private EditText tel;
    private EditText realname;
    private EditText email;
    private EditText roield;
    private RadioButton rb_student;
    private RadioButton rb_teacher;
    private Button btn_reg2;
    private ResModel model;
    private AmicoolListener<RegBean> listener;
    String roleid = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        username = (EditText) findViewById(R.id.reg_username);
        password = (EditText) findViewById(R.id.reg_password);
        realname = (EditText) findViewById(R.id.realname);
        tel = (EditText) findViewById(R.id.tel);
        email = (EditText) findViewById(R.id.email);
        rb_student = (RadioButton) findViewById(R.id.rb_student);
        rb_teacher = (RadioButton) findViewById(R.id.rb_teacher);
        btn_reg2 = (Button) findViewById(R.id.btn_reg2);
        listener = new AmicoolListener<RegBean>() {
            @Override
            public void onResponse(RegBean l) {
                if (username.length() == 0 || password.length() == 0 || tel.length() == 0 || email.length() == 0) {
                    Toast.makeText(RegActivity.this, "您的信息没填全", Toast.LENGTH_SHORT).show();
                } else if (Judgement.isEmail(email.getText().toString()) && Judgement.isTelephone(tel.getText().toString())) {
                    if (l.equals("1")) {
                        Toast.makeText(RegActivity.this, "恭喜您，注册成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(RegActivity.this, "用户名或密码已经注册过", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(RegActivity.this, "您的邮箱或者电话格式不对", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(RegActivity.this, msg, Toast.LENGTH_SHORT).show();
            }

        };
        rb_student.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roleid = "2";
            }
        });
        rb_teacher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roleid = "3";
            }
        });
        btn_reg2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model = new ResModel("gson");
                model.reg(username.getText().toString(), password.getText().toString(), tel.getText().toString(),roleid,
                        email.getText().toString(), listener);
            }
        });
    }
}

